<?php
//模块名_控制名
class Plan_RateController extends Zend_Controller_Action
{

	public function preDispatch(){
		$this->PlanRates = new Plan_Service_PlanRates();
		$this->tplDirectory = "plan/rate/";	//控制对应模板目录
	}
	
	public function indexAction(){
		$currency=Common_Service_Currency::getByCondition(array());//货币
		$warehouses = Warehouse_Service_Warehouse::getAll();	
		$this->view->warehouses=$warehouses;
		$this->view->currency=$currency;
		echo Cff::renderTpl($this->tplDirectory."index.tpl","layout");	
    }
    
	/**
	*查询套餐
	*@param page,pageSize
	*return json
	*/
	public function listAction(){
		$intpage=$this->_request->getParam('page',1);
		$pageSize=$this->_request->getParam('pageSize');
		$billtype=$this->_request->getParam('billtype'); //计费类型
		$intpage = ($intpage<1)?1:$intpage;
		$condition=array('type'=>$billtype);
		$count=$this->PlanRates->getByCondition($condition,'count(*)','',0,0);
		$data=array(
			'count'=>0,
			'ask'=>0
			);
		if($count>0){
			$result=$this->PlanRates->getByCondition($condition,'*',array('pr_id desc','type'),$pageSize,$intpage);
			foreach($result as $key=>$val){
				$whRes = Warehouse_Service_Warehouse::getById($val['wh_id']);
				$result[$key]['wh_name'] = $whRes['warehouse_name'];
			}
			$data=array(
				'count'=>$count,
				'result'=>$result,
				'ask'=>1
			);
		}
		die(Zend_Json::encode($data));
	}
    
	/**
	*修改及添加套餐
	*/
    public function editAction(){

    	$pr_id = $this->_request->getParam('pr_id','');
		$wh_id = $this->_request->getParam('wh_id','');
    	$type = $this->_request->getParam('type','');
    	$order = $this->_request->getParam('order','');
    	$range_start = $this->_request->getParam('range_start','');
    	$range_end = $this->_request->getParam('range_end','');
    	
    	$discount = $this->_request->getParam('discount','');
    	$price = $this->_request->getParam('price','');
    	$currency_code = $this->_request->getParam('currency_code','');
    	
		$rows=array(
			'wh_id'=>$wh_id,
			'type'=>$type,
			'order'=>$order,
			'range_start'=>$range_start,
			'range_end'=>$range_end,
			'discount'=>$discount,
			'price'=>$price,
			'currency_code'=>$currency_code,
		);
		
		//表单验证
		$error=array();
		$error = $this->PlanRates->validator($rows);
        if(!empty($error)){
			$return=array(
				'ask'=>0,
				'error'=>$error,
			);
            die(Zend_Json::encode($return));
        }
		if($pr_id){
			$result = $this->PlanRates->update($rows,$pr_id);
			$msg = array('ask'=>1,'msg'=>'修改成功!');
		}else{
			$result = $this->PlanRates->add($rows);
			$msg = array('ask'=>1,'msg'=>'成功添加!');
		}
		
		if($result==false){
    		$msg = array('ask'=>0,'error'=>array('操作失败!'));
    	}
		die(Zend_Json::encode($msg));
    }
    
    public function deleteAction(){
    	$pr_id = $this->_request->getParam('pr_id','');
    	if($pr_id){
    		$result =  $this->PlanRates->delete($pr_id);
    		if($result){
    			$msg = array('ask'=>1);
    		}else{
    			$msg = array('ask'=>0);
    		}
    		die(Zend_Json::encode($msg));
    	}

    }
	
    /**
	*根据套餐ID返回套餐详细
	*@param pr_id
	*return json
	*/
    public function infoAction(){
    	$pr_id = $this->_request->getParam('pr_id','');
    	if($pr_id){
    		$result = $this->PlanRates->getById($pr_id);
    		if( count($result) ){
				$data=array(
					'result'=>$result,
					'ask'=>1
				);
				die(Zend_Json::encode($data));
			}
    	}
    }
    
}